package com.lq.schoolshop.controller.before;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lq.schoolshop.bean.Result;
import com.lq.schoolshop.bean.StatusCode;
import com.lq.schoolshop.controller.BaseController;
import com.lq.schoolshop.pojo.Expand.LoveWallAndLeaveMessage;
import com.lq.schoolshop.pojo.Expand.LoveWallAndPimageAll;
import com.lq.schoolshop.pojo.LoveWall;
import com.lq.schoolshop.service.LoveWallService;
import com.lq.schoolshop.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author
 * @Date2021/1/8 21:16
 * @Version V1.0
 * 告白墙
 **/
@Api(value = "商城前台功能--评论墙模块",tags = "商城前台功能--评论墙模块")
@RequestMapping("wall")
@Controller
public class ForWallController extends BaseController {

    @Autowired
    private LoveWallService loveWallService;

    //1.存储用户输入的留言内容
    @ApiOperation(value = "插入评论墙信息",notes = "用来插入评论墙用户输入的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content",value = "留言内容",dataType = "String",required = true,paramType = "query"),
            @ApiImplicitParam(name = "puid",value = "发布商品ID",dataType = "Integer",required = true,paramType = "query"),
    })
    @ResponseBody
    @RequestMapping(value = "/InsertWallContent",method = RequestMethod.POST)
    public Result InsertWallContent(@RequestParam("content") String content,
                                    @RequestParam("puid") int puid,
                                    @RequestHeader("define-token") String language){
         System.out.println("****进入了用户正在留言板****");
         Result result = null;
         int uid = getUserIDByToken(language);
         int i = loveWallService.insertAll(content, uid, puid);
         //日志打印
         baseinfolog("用户留言","用户留言插入","影响行数:"+i);
         result = new Result(true,StatusCode.OK,"成功");
        return result;
    }


    //2.加载全部发布信息
    @ApiOperation(value = "加载发布信息",notes = "用来加载发布的所有信息")
    @ResponseBody
    @RequestMapping(value = "/ShowWallContent",method = RequestMethod.GET)
    public Result ShowWallContent(){
        Result result = null;
        List<LoveWallAndPimageAll> loveWallAndPimageAlls = loveWallService.selListByAll();
        //日志打印
        baseinfolog("用户留言","加载发布信息", JSONObject.toJSONString(loveWallAndPimageAlls));
        result = new Result(true,StatusCode.OK,"成功",loveWallAndPimageAlls);
        return result;
    }

    //3.根据发布商品ID加载用户留言信息
    @ApiOperation(value = "加载用户留言信息",notes = "根据发布商品ID加载用户留言信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页面",dataType = "Integer",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "显示数量",dataType = "Integer",required = true,paramType = "query"),
            @ApiImplicitParam(name = "puid",value = "发布商品ID",dataType = "String",required = true,paramType = "query"),
    })
    @ResponseBody
    @RequestMapping(value = "/ShowWallLeaveMessage",method = RequestMethod.GET)
    public Result ShowWallLeaveMessage(@RequestParam("pageNum") String pageNum,@RequestParam("pageSize") String pageSize,
                                       @RequestParam("puid") String puid){
        Result result = null;
        int puidint = 0;
        if(puid!=null && !puid.equals("") && pageNum!=null && !pageNum.equals("") && pageSize!=null && !pageSize.equals("")){
            puidint  = Integer.parseInt(puid);
            int pageNumInt = Integer.parseInt(pageNum);
            int pageSizeInt = Integer.parseInt(pageSize);
            List<LoveWallAndLeaveMessage> loveWallAndLeaveMessages = loveWallService.selAllmessageByPid(puidint);
            //日志打印
            baseinfolog("用户留言","加载留言信息", JSONObject.toJSONString(loveWallAndLeaveMessages));
            PageHelper.startPage(pageNumInt, pageSizeInt);
            PageInfo pageInfo = new PageInfo(loveWallAndLeaveMessages);
            result = new Result(true,StatusCode.OK,"成功",pageInfo);
        }else {
            result = new Result(true,StatusCode.OK,"失败");
        }
        return result;
    }



}
